import os
import sys
import pickle
def print_lol(the_list,indent=False,level=0,data_file=sys.stdout):
	for each_item in the_list:
		if isinstance(each_item,list):
			print_lol(each_item,indent,level+1,data_file)
		else:
			if indent:
				for tab_stop in range(level):
					print('\t',end='',file=data_file)
			print(each_item,end='',file=data_file)

os.chdir("C:\\Users\\shenhua\\Desktop\\HeadFirstPython\\Chapter3")
try:
	with open('sketch.txt') as data:
		man=[]
		other=[]
		for each_line in data:
			try:
				(role,spoken_line)=each_line.split(':',1)
				if role=='Man':
					man.append(spoken_line)
				elif role=='Other Man':
					other.append(spoken_line)
			except ValueError:
				pass
except IOError as er:
	print('IO出错:'+str(er))
try:
	with open('C:\\Users\\shenhua\\Desktop\\HeadFirstPython\\chapter4\\man_data1.txt','wb') as man_file:
		pickle.dump(man,man_file)
	with open('C:\\Users\\shenhua\\Desktop\\HeadFirstPython\\chapter4\\other_data1.txt','wb') as other_file:
		pickle.dump(other,other_file)
except IOError as er:
	print('IO出错:'+str(er))
with open('C:\\Users\\shenhua\\Desktop\\HeadFirstPython\\chapter4\\man_data1.txt','rb') as man_newfile:
	new_man=pickle.load(man_newfile)
	print_lol(new_man)

	